package com.example.javaproject.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.security.Keys;
import java.util.Date;
import java.security.Key;

public class JwtUtil {
    private static final String SECRET = "qgzxSecretqgzxSecretqgzxSecretqgzxSecret"; // 必须大于等于256位bit
    private static final Key KEY = Keys.hmacShaKeyFor(SECRET.getBytes());
    public static String createToken(Integer userId, Integer role) {
        return Jwts.builder()
            .claim("userId", userId)
            .claim("role", role)
            .setIssuedAt(new Date())
            .setExpiration(new Date(System.currentTimeMillis() + 86400000))
            .signWith(KEY, SignatureAlgorithm.HS256)
            .compact();
    }
    public static Claims parseToken(String token) {
        return Jwts.parserBuilder().setSigningKey(KEY).build().parseClaimsJws(token).getBody();
    }
} 